package top.frsf.object.card;

import lombok.Getter;
import lombok.Setter;
import top.frsf.enums.PlantTypeEnum;
import top.frsf.input.MouseAction;
import top.frsf.object.GameObject;

import java.awt.*;

/**
 * 卡片基类
 *
 * @author FRSF
 * @since 2025/8/2 22:53
 */
@Getter
@Setter
public class CardObject extends GameObject {
    /**
     * 暗色图片
     */
    protected Image darkImg;
    /**
     * 移动图片
     */
    protected Image moveImg;
    /**
     * 价格
     */
    protected int price;
    /**
     * 冷却时间
     */
    protected long cd;
    /**
     * 是否在冷却中
     */
    protected boolean inCd = false;
    /**
     * 最近一次购买时间戳
     */
    protected long lastCdTime = 0;
    /**
     * 植物类型枚举
     */
    protected PlantTypeEnum plantTypeEnum;

    /**
     * 构造函数
     *
     * @param img           图片路径
     * @param darkImg       暗色图片
     * @param moveImg       移动图片
     * @param price         价格
     * @param cd            冷却时间(ms)
     * @param plantTypeEnum 植物类型枚举
     */
    public CardObject(String img, String darkImg, String moveImg, int price, long cd, PlantTypeEnum plantTypeEnum) {
        super(img, 276, 42, 52, 72);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        this.darkImg = toolkit.getImage(darkImg);
        this.moveImg = toolkit.getImage(moveImg);
        this.price = price;
        this.cd = cd;
        this.plantTypeEnum = plantTypeEnum;
    }

    @Override
    public void paintSelfImage(Graphics g) {
        // 绘制卡片
        super.paintSelfImage(g);
        // 绘制移动图片
        if (MouseAction.moveCardObj == this) {
            Point movePoint = MouseAction.move;
            g.drawImage(moveImg, movePoint.x - 35, movePoint.y - 35, null);
        }
        // 绘制冷却图片
        cdPaint(g);
    }

    /**
     * 绘制CD
     *
     * @param g 画笔
     */
    protected void cdPaint(Graphics g) {
        if (inCd) {
            long duration = System.currentTimeMillis() - lastCdTime;
            if (duration > cd) {
                inCd = false;
            } else {
                int h = (int) ((duration / (double) cd) * getHeight());
                // CD绘制原理，目标图像的x，y, width, height 绘制到 底图的x, y, x + width, y + height
                g.drawImage(darkImg, getX(), getY(), getX() + getWidth(), getY() + h, 0, 0, getWidth(), h, null);
            }
        }
    }
}
